import {createSlice,createAsyncThunk} from '@reduxjs/toolkit';
const orderSlice = createSlice({
    name:"order",
    initialState:{
        CancelOrders:[],
        FillOrders:[],
        AllOrders:[]
    },
    reducers: {
        setCancelOrders(state,aciont){
            state.CancelOrders = aciont.payload;
        },
        setFillOrders(state,aciont){
            state.FillOrders = aciont.payload;
        },
        setAllOrders(state,aciont){
            state.AllOrders = aciont.payload;
        }
    }
})
export const {setCancelOrders,setFillOrders,setAllOrders } = orderSlice.actions
export default orderSlice.reducer;
// 3-1.获取cancel订单
export const loadCancelOrderData = createAsyncThunk(
    "order/fetchCancelOrderData",
    async(data,{dispatch})=> {
        //1.allOrder
        const {exchange} = data;
        const result = await exchange.getPastEvents("Cancel",{
            fromBlock:0,
            toBlock:"latest"
        })
        const cancelOrders = result.map(item=>item.returnValues)//取出returnValues的值
        console.log('cancelOrders',cancelOrders)
        dispatch(setCancelOrders(cancelOrders))
    })
// 3-2.获取所有订单
export const loadAllOrderData = createAsyncThunk(
    "order/fetchAllOrderData",
    async(data,{dispatch})=> {
        const {exchange} = data;
        const result = await exchange.getPastEvents("Order",{
            fromBlock:0,
            toBlock:"latest"
        })
        const allOrders = result.map(item=>item.returnValues)//取出returnValues的值
        console.log('allOrders',allOrders)
        dispatch(setAllOrders(allOrders))
    })
// 3-3.获取完成订单
export const loadFillOrderData = createAsyncThunk(
    "order/fetchFillOrderData",
    async(data,{dispatch})=> {
        const {exchange} = data;
        const result = await exchange.getPastEvents("Trade",{
            fromBlock:0,
            toBlock:"latest"
        })
        const fillOrders = result.map(item=>item.returnValues)//取出returnValues的值
        console.log('fillOrders',fillOrders)
        dispatch(setFillOrders(fillOrders))
    })